package com.wuseguang.springcloud.controller;


import com.wuseguang.springcloud.Filter.RequestParameterWrapper;
import com.wuseguang.springcloud.service.QuartzClientService;
import com.wuseguang.springcloud.service.TimetableService;
import com.wuseguang.springcloud.util.CodeUtil;
import com.wuseguang.springcloud.util.HttpRequestUtil;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 课表 前端控制器
 * </p>
 *
 * @author 
 * @since 2019-12-07
 */
@RestController
@RequestMapping("/timetable")
public class TimetableController {


    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private TimetableService timetableServiceImpl;
    @Autowired
    private QuartzClientService quartzClientService;

    /**
     * 导出课表模板
     * @param request
     * @param type 1班级课表,2全校课表
     * @return
     */
    @RequestMapping("/exportLessonTemplate")
    public void exportLessonTemplate(RequestParameterWrapper request,
                                       HttpServletResponse response,
                                       @RequestParam(value = "type",defaultValue = "1",required = false) Integer type){

        logger.info("导出课表模板 TimetableController -> exportLessonTemplate");

        //获取教师id
        String teacherId = request.getParam("teacherId");
        //获取学校id
        String schoolId = request.getParam("schoolId");
        //获取是不是管理员
        String admin = request.getParam("admin");
        //判断是否是全校课表
        if(type == 2){
            //是否是管理有员
            if(!"0".equals(admin)){
                //return new Result(CodeUtil.DO_NOT_IMPORT_THE_WHOLE_SCHOOL_TIMETABLE);
                return;
            }
        }
        int result=timetableServiceImpl.exportLessonTemplate(request,response,type,teacherId,schoolId);
       /* if(result==1){
            return new Result();
        }
        return new Result(CodeUtil.SECTION_CONTACT_ADMINISTRATOR_NOT_SET);*/

    }

    /**
     * 添加上课节次(管理员)
     * @param request
     * @param day 上课天数 （周为单位）
     * @param second 上课节次 （天为单位）
     * @param classTime 上课时间，对应节次
     * @return
     */
    @RequestMapping("/addClassDay")
    public  Result addClassDay(RequestParameterWrapper request,
                               @RequestParam(value = "day")Integer day,
                               @RequestParam(value = "second")Integer second,
                               @RequestParam(value = "classTime")String [] classTime){

        logger.info("添加上课节次 TimetableController -> addClassDay");
        /*//获取是不是管理员
        String admin = request.getParam("admin");
        //判断是否是管理员
        if(!"0".equals(admin)){
            logger.info("非管理员");
            return new Result(CodeUtil.NON_ADMINISTRATOR_UNAVAILABLE);
        }*/
        //判断上课节次跟课时是否一样
        if(second != classTime.length){
            logger.info("上课节次跟课时不一样");
            return new Result(CodeUtil.CLASS_TIME_IS_DIFFERENT_FROM_CLASS_TIME);
        }


       try {
            quartzClientService.openTask();

       }catch (Exception e){
           logger.error("创建任务失败",e);
       }
        //学校id
        String schoolId = request.getParam("schoolId");

        return timetableServiceImpl.addClassDay(schoolId,day,second,classTime);
    }


    /**
     * 检索课表
     * @param request
     * @param timetable 课表
     * @return
     */
    @RequestMapping("/searchTimetable")
    public Result searchTimetable(RequestParameterWrapper request,
                                  @RequestParam(value = "timetable",required = false) String timetable){
        logger.info("检索课表");
        //学校id
        String schoolId = request.getParam("schoolId");

        //判断是否有课表信息
        if(StringUtils.isEmpty(timetable)){
            return  new Result(CodeUtil.NO_TIMETABLE_INFORMATION);
        }

        return  timetableServiceImpl.searchTimetable(schoolId,timetable);
    }

    /**
     * 添加课表
     * @param request
     * @param timetable 课表
     * @return
     */
    @RequestMapping("/addTimetable")
    public Result addTimetable(RequestParameterWrapper request,
                               @RequestParam(value = "timetable",required = false) String timetable){
        logger.info("检索课表");
        //学校id
        String schoolId = request.getParam("schoolId");

        //判断是否有课表信息
        if(StringUtils.isEmpty(timetable)){
            return  new Result(CodeUtil.NO_TIMETABLE_INFORMATION);
        }

        return  timetableServiceImpl.addTimetable(schoolId,timetable);
    }

    /**
     *查询班级课表
     * @param request
     * @return
     */
    @RequestMapping("/findClassTimetable")
    public Result findClassTimetable(RequestParameterWrapper request,
                                     @RequestParam(value = "classid",required = false) String classid){

        logger.info("查看班级课表");
      /*  //教师id
        String  teacherId = request.getParam("teacherId");*/

        //学校id
        String schoolId = request.getParam("schoolId");

        return timetableServiceImpl.findClassTimetable(classid,schoolId);
    }


    /**
     *查询教师课表
     * @param request
     * @return
     */
    @RequestMapping("/findTecherTimetable")
    public Result findTecherTimetable(RequestParameterWrapper request){
        logger.info("查看教师课表");
        //教师id
        String  teacherId = request.getParam("teacherId");

        return timetableServiceImpl.findTecherTimetable(teacherId);
    }

    /**
     * 查询班主任班级课表
     * @param request
     * @return
     */
    @RequestMapping("/findClassSchedule")
    public Result findClassSchedule(RequestParameterWrapper request){
        logger.info("查询班主任班级课表");
        //教师id
        String  teacherId = request.getParam("teacherId");
        //学校id
        String schoolId = request.getParam("schoolId");
        return timetableServiceImpl.findClassSchedule(teacherId);
    }

    /**
     * 查询学校的上课节次
     * @param request
     * @return
     */
    @RequestMapping("/findClassDay")
    public Result findClassDay(RequestParameterWrapper request){
        //学校id
        String schoolId = request.getParam("schoolId");
        logger.info("查询上课节次");
        return timetableServiceImpl.findClassDay(schoolId);
    }

    /**
     * 查询这个当前时间老师的课表
     * @param request
     * @return
     */
    @RequestMapping("/findDateTimetable")
    public Result findDateTimetable(RequestParameterWrapper request,
                                    @RequestParam(value = "time") String time){
        logger.info("查询这个当前时间老师的课表");
        //教师id
        String  teacherId = request.getParam("teacherId");
        //学校id
        String schoolId = request.getParam("schoolId");
        return timetableServiceImpl.findDateTimetable(teacherId,time,schoolId);
    }
}

